package com.dataseek.iot.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.product.entity.PdtAttrRel;
import com.dataseek.iot.product.entity.PdtList;
import com.dataseek.iot.product.mapper.PdtAttrRelMapper;
import com.dataseek.iot.product.mapper.PdtAttributeDicMapper;
import com.dataseek.iot.product.mapper.PdtListMapper;
import com.dataseek.iot.product.service.PdtListService;
import com.dataseek.iot.product.vo.PdtListVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import com.dataseek.iot.toolkit.mybatis.entity.DeviceList;
import com.dataseek.iot.toolkit.mybatis.mapper.DeviceListMapper;
import com.dataseek.iot.toolkit.tenant.config.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class PdtListServiceImpl implements PdtListService {
    @Autowired
    private IDServiceHelper idServiceHelper;

    @Autowired
    private PdtListMapper pdtListMapper;


    @Autowired
    private PdtAttributeDicMapper pdtAttributeDicMapper;

    @Autowired
    private PdtAttrRelMapper pdtAttrRelMapper;


    @Autowired
    private DeviceListMapper deviceListMapper;

    @Override
    public PdtListVo queryPdtList(String pdtName, String ctgCode, Integer num, Integer size) {
        List<PdtList> list = new ArrayList<>();
        PdtListVo pdtListVo = new PdtListVo();
        QueryWrapper<PdtList> qw = new QueryWrapper<>();
        if (pdtName != null && !pdtName.isEmpty()) {
            qw.like("pdt_name", pdtName);
        }
        if (ctgCode != null && !ctgCode.isEmpty()) {
            qw.like("ctg_code", ctgCode);
        }
        IPage<PdtList> page = new Page<>(num, size);
        List<PdtList> mapList = pdtListMapper.selectPage(page, qw).getRecords();
        for (int i = 0; i < mapList.size(); i++) {
            PdtList pdtList = mapList.get(i);
            String pdtCode = pdtList.getPdtCode();
            QueryWrapper<DeviceList> qwDevice = new QueryWrapper<>();
            qwDevice.like("pdt_code", pdtCode);
            Integer integer = deviceListMapper.selectCount(qwDevice);
            pdtList.setDeviceCounts(integer);
            list.add(pdtList);
        }
        pdtListVo.setPage(num);
        pdtListVo.setSize(size);
        pdtListVo.setTotal(page.getTotal());
        pdtListVo.setList(list);
        return pdtListVo;
    }

    //新增部门信息
    @Override
    public Integer insertPdtList(PdtList pdtList) {
        String tenantCode = DynamicDataSourceContextHolder.getDataSourceKey();
        int resultCode = 0;
        long id = idServiceHelper.nextId();
        pdtList.setId(id);
        String code = pdtList.getPdtName();
        QueryWrapper<PdtList> qw = new QueryWrapper<>();
        qw.eq("pdt_name", code);
        Integer count = pdtListMapper.selectCount(qw);
        if (count == 0) {
            // pdtAttributeDic.setTenantCode(tenantCode);
            pdtListMapper.insert(pdtList);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updatePdtList(PdtList pdtList) {
        int resultCode = pdtListMapper.updateById(pdtList);
        return resultCode;
    }

    @Override
    public Integer deletePdtList(List<Long> ids) {
        Integer resultCode = 0;
        Integer count = 0;
        for (int i = 0; i < ids.size(); i++) {
            long id = ids.get(i);
            PdtList pdtList = pdtListMapper.selectById(id);
            String pdtCode = pdtList.getPdtCode();
            QueryWrapper<DeviceList> qw = new QueryWrapper<>();
            qw.eq("pdt_code", pdtCode);
            count = count + deviceListMapper.selectCount(qw);
        }
        if (count > 0) {
            resultCode = RspStatusEnum.FAIL.getCode();
        } else {
            resultCode = pdtListMapper.deleteBatchIds(ids);
        }
        return resultCode;
    }

}
